package view;

import controller.Client;
import model.User;
import service.LoginCheck;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

import static javax.swing.SwingConstants.CENTER;

/**
 * @author ASUS
 */
public class Login extends JFrame implements ActionListener {
    public static void main(String[] args) {
        new Login();
    }

    JLabel jl1, jl2, jl3, jl4, jl5;
    JButton log, close;
    JCheckBox jcb1, jcb2;
    JTextField id;
    JPasswordField pwd;
    int check = 0;


    public Login() {

        //登陆界面
        jl1 = new JLabel(new ImageIcon(getClass().getResource("/qq1.png")));
        jl1.setBounds(0, 0, 533, 223);
        jl1.setFont(new Font("宋体", 10, 16));
        this.add(jl1);
        jl2 = new JLabel("账号", CENTER);
        jl2.setBounds(80, 237, 50, 50);
        jl2.setFont(new Font("宋体", 10, 16));
        jl3 = new JLabel("密码", CENTER);
        jl3.setBounds(80, 287, 50, 50);
        jl3.setFont(new Font("宋体", 10, 16));
        jl4 = new JLabel("注册账号");
        jl4.setBounds(400, 235, 150, 50);
        jl4.setFont(new Font("宋体", 10, 16));
        jl4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        jl4.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new Resgister();
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        jl5 = new JLabel("找回密码");
        jl5.setBounds(400, 290, 150, 50);
        jl5.setFont(new Font("宋体", 10, 16));
        jl5.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        //登录设置监听
        log = new JButton("   登 录    ");
        log.setBounds(170, 400, 190, 30);
        log.setFont(new Font("宋体", 10, 16));
        log.setBackground(Color.CYAN);
        log.addActionListener(this);
        //关闭设置监听
        close = new JButton(new ImageIcon(getClass().getResource("/3.png")));
        close.setBounds(497, 0, 35, 36);
        close.addActionListener(this);

        jcb1 = new JCheckBox("记住密码");
        jcb1.setBounds(120, 340, 150, 50);
        jcb1.setFont(new Font("宋体", 10, 16));
        jcb2 = new JCheckBox("自动登录");
        jcb2.setBounds(330, 340, 150, 50);
        jcb2.setFont(new Font("宋体", 10, 16));

        id = new JTextField(10);
        id.setBounds(170, 246, 190, 30);
        pwd = new JPasswordField(10);
        pwd.setBounds(170, 300, 190, 30);

        add(jl1);
        add(jl2);
        add(jl3);
        add(jl4);
        add(jl5);
        add(close);
        add(log);
        add(jcb1);
        add(jcb2);
        add(id);
        add(pwd);

        setLayout(null);
        setSize(530, 450);
        setLocation(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setUndecorated(true);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == close) {
            System.exit(0);
        }

        if (e.getSource() == log) {
            //登录成功 拿到输入的ID
            User user = new User();
            user.setId(Integer.parseInt(id.getText()));
            user.setPwd(pwd.getText());
            //检查数据库中是否有改用户
            check = new LoginCheck().check(user);
            Socket socket = null;
            if (check == 1) {
                try {
                    //登录成功 让用户ID发到服务器
                    socket = new Client().connect();
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                    objectOutputStream.writeObject(user.getId());
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                this.dispose(); //登录成功后关闭该窗口
                //登录成功过启动好友列表界面
                new FriendList(user.getId(), socket);
                System.out.println("登录成功");
            } else {
                System.out.println("账号或密码错误");
            }
        }
    }
}
